﻿using System;
using System.Collections.Generic;
using CfgTable;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay
{
    /// <summary>
    /// 配置表扩展工具
    /// </summary>
    public static class TableExtension
    {
        static public int CountList(this TbCfgEquipSkin tbCfg, int itemCid)
        {
            if (tbCfg.itemIdList.TryGetValue(itemCid, out var list))
            {
                return list.Count;
            }
            return 0;
        }

        static public CfgEquipSkin GetCfg(this TbCfgEquipSkin tbCfg, int itemCid, int index)
        {
            if (tbCfg.itemIdList.TryGetValue(itemCid, out var list))
            {
                return index < list.Count ? tbCfg.Get(list[index]) : null;
            }
            return null;
        }

        static public CfgEquipStar MaxCfg(this TbCfgEquipStar tbCfg, int itemCid)
        {
            if (tbCfg.itemIdStarMap.TryGetValue(itemCid, out var map))
            {
                int star = -1;
                int cid = -1;
                foreach (var kv in map)
                {
                    if (star < kv.Key)
                    {
                        star = kv.Key;
                        cid = kv.Value;
                    }

                }
                if (cid != -1)
                    return tbCfg.Get(cid);
            }
            return null;
        }

        static public CfgEquipStar GetCfg(this TbCfgEquipStar tbCfg, int itemCid, int star)
        {
            if (tbCfg.itemIdStarMap.TryGetValue(itemCid, out var map))
            {
                if (map.TryGetValue(star, out var starCid))
                {
                    return tbCfg.Get(starCid);
                }
            }
            return null;
        }

        static public CfgSkillLevel MaxCfg(this TbCfgSkillLevel tbCfg, int skillCid)
        {
            if (tbCfg.skillIdLvMap.TryGetValue(skillCid, out var map))
            {
                int lv = -1;
                int cid = -1;
                foreach (var kv in map)
                {
                    if (lv < kv.Key)
                    {
                        lv = kv.Key;
                        cid = kv.Value;
                    }

                }
                if (cid != -1)
                    return tbCfg.Get(cid);
            }
            return null;
        }

        static public CfgSkillLevel GetCfg(this TbCfgSkillLevel tbCfg, int skillCid, int skillLv)
        {
            if (tbCfg.skillIdLvMap.TryGetValue(skillCid, out var map))
            {
                if (map.TryGetValue(skillLv, out var lvCid))
                {
                    return tbCfg.Get(lvCid);
                }
            }
            return null;
        }

        static public CfgHeroStar MaxCfg(this TbCfgHeroStar tbCfg, int heroCid)
        {
            if (tbCfg.heroIdStarMap.TryGetValue(heroCid, out var map))
            {
                int star = -1;
                int cid = -1;
                foreach (var kv in map)
                {
                    if (star < kv.Key)
                    {
                        star = kv.Key;
                        cid = kv.Value;
                    }

                }
                if (cid != -1)
                    return tbCfg.Get(cid);
            }
            return null;
        }

        static public CfgHeroStar GetCfg(this TbCfgHeroStar tbCfg, int heroCid, int star)
        {
            if (tbCfg.heroIdStarMap.TryGetValue(heroCid, out var map))
            {
                if (map.TryGetValue(star, out var cfgId))
                {
                    return tbCfg.Get(cfgId);
                }
            }
            return null;
        }

        static public CfgHeroAwaken MaxCfg(this TbCfgHeroAwaken tbCfg, int heroCid)
        {
            if (tbCfg.heroIdAwakenLvMap.TryGetValue(heroCid, out var map))
            {
                int awakenLv = -1;
                int cid = -1;
                foreach (var kv in map)
                {
                    if (awakenLv < kv.Key)
                    {
                        awakenLv = kv.Key;
                        cid = kv.Value;
                    }

                }
                if (cid != -1)
                    return tbCfg.Get(cid);
            }
            return null;
        }

        static public CfgHeroAwaken GetCfg(this TbCfgHeroAwaken tbCfg, int heroCid, int awakenLv)
        {
            if (tbCfg.heroIdAwakenLvMap.TryGetValue(heroCid, out var map))
            {
                if (map.TryGetValue(awakenLv, out var cfgId))
                {
                    return tbCfg.Get(cfgId);
                }
            }
            return null;
        }

        static public CfgQuest GetParentCfg(this TbCfgQuest tbCfg, int questCid)
        {
            if (tbCfg.child2ParentMap.TryGetValue(questCid, out var parentCid))
            {
                return tbCfg.Get(parentCid);
            }
            return null;
        }

        static public CfgPortablePart GetCfgByItemIdAndPartId(this TbCfgPortablePart tbCfg, int itemId, int partId)
        {
            if (tbCfg.portablItemPartMap.TryGetValue(itemId, out var partMap))
            {
                if (partMap.TryGetValue(partId, out var cid))
                {
                    return tbCfg.Get(cid);
                }
            }
            return null;
        }

        static public IReadOnlyList<int> GetMallMainTypes(this TbCfgMallType tbCfg, MallOriginalType originalType)
        {
            if (tbCfg.MallFuncTypeMap.TryGetValue(originalType, out var map))
            {
                List<int> types = ListPool<int>.Get();
                foreach (var mainType in map.Keys)
                {
                    if (!types.Contains(mainType))
                    {
                        types.Add(mainType);
                    }
                }
                var rets = types.ToArray();
                ListPool<int>.Put(types);
                return rets;

            }
            return Array.Empty<int>();
        }

        static public IReadOnlyList<int> GetMallSubTypeCids(this TbCfgMallType tbCfg, MallOriginalType originalType, int mainType)
        {
            if (tbCfg.MallFuncTypeMap.TryGetValue(originalType, out var map))
            {
                if (map.TryGetValue(mainType, out var mallTypeCids))
                {
                    return mallTypeCids;
                }
            }
            return Array.Empty<int>();
        }

        static public IReadOnlyList<int> GetCids(this TbCfgHeroFateTrajectory tbCfg, int heroCid, int group)
        {
            if (tbCfg.heroIdGroupIdList.TryGetValue(heroCid, out var map))
            {
                if (map.TryGetValue(group, out var list))
                    return list;
            }
            return Array.Empty<int>();
        }

        static public CfgHeroFateTrajectory GetCfg(this TbCfgHeroFateTrajectory tbCfg, int heroCid, int stage)
        {
            if (tbCfg.heroIdStageMap.TryGetValue(heroCid, out var map))
            {
                if (map.TryGetValue(stage, out var cid))
                    return tbCfg.Get(cid);
            }
            return null;
        }

        static public int MaxStage(this TbCfgHeroFateTrajectory tbCfg, int heroCid)
        {
            if (tbCfg.heroIdStageMap.TryGetValue(heroCid, out var map))
            {
                return map.Count;
            }
            return 0;
        }
    }
}
